package com.cyt.web.controller.work.wb.wbroom;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;

import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.sys.util.ExcelTest;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.wb.model.po.WxWbBulid;
import com.cyt.work.wb.model.po.WxWbRoom;
import com.cyt.work.wb.model.vo.WxWbBulidVo;
import com.cyt.work.wb.model.vo.WxWbRoomExportVo;
import com.cyt.work.wb.model.vo.WxWbRoomVo;
import com.cyt.work.wb.service.WxWbBulidService;
import com.cyt.work.wb.service.WxWbRoomService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 维保模块-房间表控制层
 *
 * @author ding xu
 * @date 2019-11-27 17:42:26
 */
@RestController
public class WxWbRoomController extends BaseController {

    @Resource
    private WxWbRoomService wxWbRoomService;
    @Resource
    private WxWbBulidService wxWbBulidService;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author ding xu
     * @date 2019-11-27 17:42:26
     */
    @RequestMapping("/work/wb/wxwbroom/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 分页查询
     *
     * @param wxWbRoomVo 接受参数对象
     * @param page 当前页面
     * @param limit 页面数量
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-27 17:42:26
     */
    @BaseLog(module = "维保模块", classz = "房间表", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wb/wxwbroom/page", method = RequestMethod.POST)
    public ReturnResult page(WxWbRoomVo wxWbRoomVo, int page, int limit) {
        return wxWbRoomService.page(wxWbRoomVo, page, limit);
    }



    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-27 17:42:26
     */
    @BaseLog(module = "维保模块", classz = "房间表", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wb/wxwbroom/getAdd", method = RequestMethod.GET)
    public ModelAndView getEdit(String id,String bulidId,String applyId) {
        ModelAndView view = new ModelAndView("/work/wb/wxwbroom/wbroomadd");
        if (StringUtils.isNotBlank(id)) {
            WxWbRoom wxWbRoom=wxWbRoomService.selectByPrimaryKey(id);
            if (StringUtils.isNotBlank(wxWbRoom.getBulidId())) {
                WxWbBulid wxWbBulid=wxWbBulidService.selectByPrimaryKey(wxWbRoom.getBulidId());
                wxWbRoom.setBulidName(wxWbBulid.getName());
            }
            wxWbRoom.setApplyId(applyId);
            view.addObject(wxWbRoom);
        }
        if(StringUtils.isNotBlank(bulidId)){
            WxWbBulid wxWbBulid=wxWbBulidService.selectByPrimaryKey(bulidId);
            WxWbRoom wxWbRoom =new WxWbRoom();
            wxWbRoom.setBulidName(wxWbBulid.getName());
            wxWbRoom.setApplyId(applyId);
            wxWbRoom.setBulidId(bulidId);
            view.addObject(wxWbRoom);
        }
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param wxWbRoomVo 接受参数对象
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-27 17:42:26
     */
    @BaseLog(module = "维保模块", classz = "房间表", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/wb/wxwbroom/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WxWbRoomVo wxWbRoomVo) {
        return wxWbRoomService.addOrUpdate(request,wxWbRoomVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request 请求对象
     * @param wxWbRoomVo 接受参数对象
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-27 17:42:26
     */
    @BaseLog(module = "维保模块", classz = "房间表", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wb/wxwbroom/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WxWbRoomVo wxWbRoomVo) {
        return wxWbRoomService.remove(wxWbRoomVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request 请求对象
     * @param wxWbRoomVo 接受参数对象
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-27 17:42:26
     */
    @BaseLog(module = "维保模块", classz = "房间表", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wb/wxwbroom/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, WxWbRoomVo wxWbRoomVo) {
        return wxWbRoomService.delete(wxWbRoomVo, acceptCurrentUser(request));
    }

    /**
     * 导出房间模板
     * @param response
     * @author hanjunW
     * @date 2019/9/3 13:43
     */
    @RequestMapping(value = "/work/wxwbroom/export")
    public void exportExcel(HttpServletResponse response,String applyId) throws IOException {
        List<WxWbRoomVo> list = new ArrayList<>();

        // 设置联动模板
        List<WxWbBulid> listBulids=wxWbBulidService.findList(new WxWbBulidVo(applyId));
        String[] provinces =new String[listBulids.size()];
        for(int i=0;i<listBulids.size();i++){
            provinces[i]=listBulids.get(i).getExcelModel();
        }
        EasyPoiUtil.exportExcelModel(list,"房间信息.xls",provinces,response,WxWbRoomExportVo.class);
    }

    /**
     * 导入房间信息
     * @param file
     * @param request
     * @author hanjun
     * @date 2019/9/3 13:33
     */
    @RequestMapping(value = "/work/wxwbroom/importExcel", method = RequestMethod.POST)
    public ReturnResult importExcel(MultipartFile file, HttpServletRequest request, String applyId) {
        return wxWbRoomService.importExcel(request,file, acceptCurrentUser(request),applyId);
    }
}